package com.sortingalgorithm;

import java.util.Arrays;

/*
* 冒泡
* */
public class BubbleSort {
    public static void main(String[] args) {
        int[] arr = {1,3,6,7,2,54,54,4};
        bubbleSort(arr);
        System.out.println(Arrays.toString(arr));
    }

    public static void bubbleSort(int [] arr){
        if(arr == null || arr.length < 2) {
            return;
        }
        for(int i = 0;i < arr.length - 1;i++){
            for(int j = arr.length - 1; j > i;j--){
                if(arr[j - 1] > arr[j]){
                    swap(arr,j-1,j);
                }
            }
        }
    }

    private static void swap(int[] arr,int i,int j){
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

}
